// <copyright file="DashboardController.cs" company="Clever Software">
// Copyright (c) 2013, 2014 All Right Reserved, http://www.cleversoftware.by
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Clever Software</author>
// <email>info@cleversoftware.by</email>
// <date>2014-02-04</date> 
// <summary>CleverCMS source code</summary>

namespace CleverCMS.Web.Cms.Admin.Controllers
{
    using System.Linq;
    using System.Web.Mvc;

    using CleverCMS.Blog.Services;
    using CleverCMS.Cms.Services;
    using CleverCMS.Cms.Services.ViewModels;

    [Authorize(Roles = "Admin")]
    public class DashboardController : Controller
    {
        private readonly AccountService             m_accountService;
        private readonly PageService                m_pageService;
        private readonly BlogEntryService           m_blogEntryService;
        private readonly MenuItemService            m_menuItemService;
        private readonly SettingService             m_settingService;
        private readonly MonitorService             m_monitorService;

        public DashboardController(AccountService   accountService,
                                   PageService      pageService,
                                   BlogEntryService blogEntryService,
                                   MenuItemService  menuItemService,
                                   SettingService   settingService,
                                   MonitorService   monitorService)
        {
            this.m_accountService = accountService;
            this.m_pageService = pageService;
            this.m_blogEntryService = blogEntryService;
            this.m_menuItemService = menuItemService;
            this.m_settingService = settingService;
            this.m_monitorService = monitorService;
        }

        public ActionResult Index()
        {
            var model = new DashboardViewModel
            {
                Users = m_accountService.LoadAllUsers().Count(),
                Pages = m_pageService.LoadAll().Count(),
                Blogs = m_blogEntryService.GetAll().Count(),
                Menues = m_menuItemService.LoadAllMenuItems().Count(),
                Widgets = m_settingService.LoadAll().Count(),
                Logs = m_monitorService.LoadTopFiveErrors().ToList(),
                PagesViewCount = m_pageService.GetTotalViewCount(),
                PostsViewCount = m_blogEntryService.GetTotalViewCount()
            };
            return View(model);
        }
    }
}